import Cookies from 'js-cookie'
import service from './request'

const TokenKey = 'goexam_admin_token'

export function getToken() {
  return Cookies.get(TokenKey)
  // return localStorage.getItem(TokenKey)
}

export function setToken(token) {
  console.log('set token to cookies', token)
  service.interceptors.request.use(
    config => {
      config.headers['Authorization'] = token
      config.headers['content-type'] = 'application/json'
      return config
    },
    error => {
      // do something with request error
      console.log(error) // for debug
      return Promise.reject(error)
    }
  )
  return Cookies.set(TokenKey, token, { expires: 1, path: '/' })
}

export function removeToken() {
  return Cookies.remove(TokenKey)
  // return localStorage.removeItem(TokenKey)
}
